<?php

namespace App\Http\Controllers;

use App\Models\Analysis;
use App\Models\ESCluster;
use Illuminate\Http\Request;

class AnalysisController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(Request $request)
    {
        
        $e_s_unit_id = $request->input('e_s_unit_id'); 
        if($e_s_unit_id){        
            $cluster_ids = ESCluster::where('e_s_unit_id', $e_s_unit_id)->pluck('id');
            $data = Analysis::requestCriteria($request)->where('dev_type', 'App\Models\ESCluster')->whereIn('dev_id', $cluster_ids)
            ->paginate($request->query('limit'));
        }else{
            $data = Analysis::requestCriteria($request)->paginate($request->query('limit'));
        }

        return $this->respondSuccess($data);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'dev_id' => 'required',
            'type' => 'required|in:SOC校正,SOH校正',
            // 'description' => 'required',
            'result' => 'required',
            'user_name'=> 'max:50',
            // 'user_id' => ,
            'begin_at' => 'required',
            'end_at' => 'required',
            // 'remarks' , 
            'correction_id' ,
        ]);
        $request['dev_type'] = 'App\Models\ESCluster';
        $analysis = Analysis::create($request->input());
        return $this->respondSuccess($analysis);
    }

    /**
     * Display the specified resource.
     */
    public function show(Analysis $analysis)
    {
        return $this->respondSuccess($analysis);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, Analysis $analysis)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Analysis $analysis)
    {
        //
    }
}
